<?php
class BaseModelDBDAL {
    /**
     * select sql query building
     * @param string $dbName
     * @param string $table
     * @param mixed array $where
     * @param int $sn
     * @param int $per
     * @param mixed array $order
     * @return mixed data or false on failure
     */
	public static function get($dbName, $table, $where, $sn, $per, $order = array()) {
		$sn = intval($sn);
		$per = $per ? intval($per) :1;
		$sql = "SELECT * FROM $table";
		$sql .= self::sqlWhere($where);
		$sql .= self::sqlOrder($order);
		$sql .= " LIMIT $sn, $per";
		$db = new DB($dbName);
		$ret = $db->getAttributes($sql, $data);
		return $ret ? $data : false;
	}
    /**
     * select fields sql query building
     * @param string $dbName
     * @param string $table
     * @param mixed array $where
     * @param int $sn
     * @param int $per
     * @param mixed array $order
     * @return mixed data or false on failure
     */
	public static function getFields($dbName, $table, $fields, $where, $sn, $per, $order = array()) {
		$sn = intval($sn);
		$per = $per ? intval($per) :1;

		$sql = "SELECT " . self::sqlFields($fields) . " FROM $table";
		$sql .= self::sqlWhere($where);
		$sql .= self::sqlOrder($order);
		$sql .= " LIMIT $sn, $per";
		$db = new DB($dbName);
		$ret = $db->getAttributes($sql, $data);
		return $ret ? $data : false;
	}
	/**
	 * count number
	 * @param string $dbName
	 * @param string $table
	 * @param mixed array $where
	 * @return int number or false on failure
	 */
	public static function count($dbName, $table, $where) {
		$sql = "SELECT COUNT(1) AS num FROM $table";
		$sql .= self::sqlWhere($where);
		$db = new DB($dbName);
		$ret = $db->getOneAttribute($sql, $num);
		return $ret ? $num : false;
	}
	/**
	 * delete query building
	 * @param string $dbName
	 * @param string $table
	 * @param mixed $where
	 * @return bool
	 */
	public static function delete($dbName, $table, $where) {
	    $sql = "DELETE FROM $table";
	    $sql .= self::sqlWhere($where);
	    $db = new DB($dbName);
	    return $db->runQuery($sql);
	}
	/**
	 * update query building
	 * @param string $dbName
	 * @param string $table
	 * @param mixed array $sets
	 * @param mixed array $where
	 */
	public static function update($dbName, $table, $sets, $where) {
	    $set = self::sqlSets($sets);

	    if (empty($set)) {
	        return false;
	    }
	    $sql = "UPDATE $table SET ".$set." ".self::sqlWhere($where);
	    $db = new DB($dbName);
	    return $db->runQuery($sql);
	}
	/**
	* generator sql where partern
	* @params mixed arrary $where
	* @return string
	* example: array('uid' => array('opt' => '>=',
	                                'val' => '1234292',
							  ),
				'stattime' => array(
				                    array('opt' => '>=',
				                          'val' => '2010-09-03'),
								    array('opt' => '<=',
									      'val' => '2011-03-01')
							  ),
				)
	*/
	public static function sqlWhere($where) {
		if (empty($where) || !is_array($where)) {
			return '';
		}
		$tmp = array();
		foreach ($where as $key => $val) {
		    $opt = '=';
			$dat = $val;
			if (is_array($val)) {
				if (isset($val['opt'])) {
					$opt = $val['opt'];
					$dat = $val['val'];
				}
				else {
					foreach ($val as $dal) {
						$tmp[] = "{$key} ".$dal['opt']." '".$dal['val']."'";
					}
					continue;
				}
			}
			if ($opt == 'in') {
			    $tmp[] = "{$key} {$opt} ({$dat})";
			}
            else if ($opt == 'like') {
			    $tmp[] = "{$key} {$opt} {$dat}";
            }
            else {
			    $tmp[] = "{$key} {$opt} '{$dat}'";
            }
		}
		if (empty($tmp)) {
			return '';
		}
		return  " WHERE ".implode(" AND ", $tmp);
	}
	/**
	 * generate the sql order
	 * @param mixed array $order
	 * @return string
	 */
	public static function sqlOrder($order) {
		if (empty($order) || !is_array($order)) {
			return '';
		}
		$tmp = array();
		foreach ($order as $key => $val) {
			$tmp[] = $key." ".$val;
		}
                if (empty($tmp)) {
                    return '';
                }
		return " ORDER BY ".implode(" , ", $tmp);
	}
	/**
	 * generate the update sql sets
	 * @param mixed array $sets
	 */
	public static function sqlSets($sets) {
	    if (!is_array($sets) || empty($sets)) {
	        return '';
	    }
	    foreach ($sets as $key => $val) {
			$_tmp[] = "$key='".addslashes($val)."'";
		}
		return implode(',', $_tmp);
	}
	/**
	 * generate the sql fields
	 * @param mixed array $fields
	 * @return string
	 */
	public static function sqlFields($fields){
		if (empty($fields)) {
			return '*';
		}
		foreach ($fields as $k => $f) {
			$fields[$k] = '`'.addslashes($f).'`';
		}
		return implode(',', $fields);
	}
}
?>
